package dao;

import help.Utils;
import java.util.ArrayList;
import java.util.List;
import dto.PatientDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PatientDAO extends DatabaseHelper {
	
    public static final String TABLE_PATIENT = "Patient";
    
    public static final String PATIENT_ID = "ID";
    public static final String PATIENT_FIRSTNAME = "FirstName";
    public static final String PATIENT_SURNAME = "SurName";
    public static final String PATIENT_AGE = "Age";
    public static final String PATIENT_GENDER = "Gender";
    
	public PatientDAO(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}

    public int addPatient(PatientDTO patientDTO) 
    {
    	// Open database
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(PATIENT_FIRSTNAME, patientDTO.getFirstName());
	        values.put(PATIENT_SURNAME, patientDTO.getSurname());
	        values.put(PATIENT_AGE, patientDTO.getAge());
	        values.put(PATIENT_GENDER, patientDTO.getGender());
	        
	        // Inserting rows
	        open();
	        long id = DatabaseHelper.myDataBase.insert(TABLE_PATIENT, null, values);
	
	        // Close database connection
	        DatabaseHelper.myDataBase.close();
	        return (int)id;
        }
        catch (Exception ex) { return -1;}
    }         
    
    public PatientDTO getPatient(int id) {
        try {
	        Cursor cursor = DatabaseHelper.myDataBase.query(TABLE_PATIENT, new String[] { PATIENT_ID,
	        		PATIENT_FIRSTNAME, PATIENT_SURNAME, PATIENT_AGE, PATIENT_GENDER}, PATIENT_ID + "?",
	            new String[] {String.valueOf(id) }, null, null, null, null);

	        if (cursor != null)
	            cursor.moveToFirst();
	        
	        PatientDTO patient = new PatientDTO(
	                Integer.parseInt(cursor.getString(0)),
	                cursor.getString(1),
	                cursor.getString(2),
	                Integer.parseInt(cursor.getString(3)),
	                Boolean.parseBoolean(cursor.getString(4)));
	        return patient;
        }
        catch (Exception e) {
        	return null;
        }
    }
        
    public List<PatientDTO> getAllPatients() {
    	try {
	        List<PatientDTO> patientList = new ArrayList<PatientDTO>();
	        
	        // Select all query
	        String selectQuery = "SELECT * FROM " + TABLE_PATIENT;
	        open();
	        Cursor cursor = DatabaseHelper.myDataBase.rawQuery(selectQuery, null);
	        
	        // Looping through all rows and adding to list
	        
	        if (cursor.moveToFirst()) {
	            do {
	            	PatientDTO patientDTO = new PatientDTO(
	                		Integer.parseInt(cursor.getString(0)),
	                        cursor.getString(1),
	                        cursor.getString(2),
	                        Integer.parseInt(cursor.getString(3)),
	                        Utils.Int2Boolean(Integer.parseInt(cursor.getString(4))));
	            
	            	patientList.add(patientDTO);
	            } while (cursor.moveToNext());
	        }
	        close();
	        return patientList;
    	}
    	catch (Exception e) {
			return null;
		}
    }
    
    public boolean updatePatient(PatientDTO patientDTO) {
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(PATIENT_ID, patientDTO.getID());
	        values.put(PATIENT_FIRSTNAME, patientDTO.getFirstName());
	        values.put(PATIENT_SURNAME, patientDTO.getSurname());
	        values.put(PATIENT_AGE, patientDTO.getAge());
	        values.put(PATIENT_GENDER, patientDTO.getGender());
	        open();
	        // Updating row
	        int isSucceed = DatabaseHelper.myDataBase.update(TABLE_PATIENT, values, PATIENT_ID + " = ?",
	                new String[] { String.valueOf(patientDTO.getID()) });
	        close();
	        if (isSucceed > 0) {
	        	return true;
	        }
	        	
	        return false;
        }
        catch (Exception ex) { 
        	return false; 
        }
    }
    
    public void deletePatient(PatientDTO patientDTO) {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_PATIENT, PATIENT_ID + " = ?",
                new String[] { String.valueOf(patientDTO.getID()) });
    		DatabaseHelper.myDataBase.close();
    	}
    	catch (Exception ex) {}
    }
    
    public void deleteAllPatients() 
    {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_PATIENT, null, null);
    	}
    	catch (Exception ex) {}
    }
    
    public int getPatientCount()
    {
    	try {
	        String countQuery = "SELECT * FROM " + TABLE_PATIENT;
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	        
	        // Return count
	        return cursor.getCount();
    	}
    	catch (Exception ex) {return -1;}
    }
}